package com.netx.cubigraf.servlets;
import java.io.IOException;
import com.netx.cubigraf.entities.Precos;
import com.netx.ebs.EbsRequest;
import com.netx.ebs.EbsResponse;
import com.netx.ebs.TemplateValues;
import com.netx.generics.sql.Table;
import com.netx.data.Connection;
import com.netx.data.DatabaseException;
import com.netx.cubigraf.entities.Entities;


public class SrvGestaoPrecario extends CubigrafServlet {

	public SrvGestaoPrecario() {
		super("gestao-precario");
	}

	protected void doGet(EbsRequest request, EbsResponse response) throws IOException, DatabaseException {
		Connection c = request.getDatabaseConnection();
		Table precos = Entities.getPrecos(c).procurarPrecos();
		c.close();
	
		TemplateValues tv = new TemplateValues();
		for(int i=0; i<15; i++) {
			if(precos.getString(2, i) != null) {
				tv.put("preco"+(i+1), precos.getDouble(2, i));
			}
			else {
				tv.put("preco"+(i+1), "");
			}
		}

		response.setContentType("text/html");
		response.sendDisableCache();
		response.sendTemplate("gestao-precario.ficha.tplt", tv);
	}

	protected void doPost(EbsRequest request, EbsResponse response) throws IOException, DatabaseException {
		Connection c = request.getDatabaseConnection();
		Precos precos = Entities.getPrecos(c);
		for(int i=0; i<15; i++) {
			Double preco = null;
			if(request.getParameter("preco"+(i+1)) != null) {
				preco = request.getDoubleParameter("preco"+(i+1));
			}
			precos.actualizar((i+1), preco);
		}
		c.close();
		response.sendRedirect("gestao-precario");
	}

}
